﻿
@{
    ViewData["Title"] = "UptStorehouse";
    Layout = "~/Views/Shared/Temp.cshtml";
}


<blockquote class="layui-elem-quote">
    <h1 style="color:darkgray">编辑库位</h1>
</blockquote>
<div class="layui-card" style="width:80%;margin:auto;">
    <div class="layui-card-header">
        <table style="border:1px solid darkgray;width:100%;height:50px;margin-top:30px">
            <tr>
                <td style="color:darkgray">新建库位</td>
            </tr>
        </table>
    </div>
    <div class="layui-card-body">
        <form class="layui-form" id="myForm" action="" style="margin-top: 30px;" width="600px">
            <div class="layui-row" style="margin-bottom: 20px;">
                <div class="layui-col-xs6">
                    <label class="layui-form-label">库位编号</label>
                    <div class="layui-input-block">
                        <input type="text" id="storehouseCode" name="storehouseCode" lay-verify="storehouseCode" placeholder="请输入" autocomplete="off" class="layui-input" style="width:150px" disabled>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">仓库</label>
                    <div class="layui-input-block" style="width:150px">
                        <select name="warehouseId" lay-filter="仓库" lay-verify="warehouseId" id="warehouseId" placeholder="请选择仓库">
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-row" style="margin-bottom: 20px;">
                <div class="layui-col-xs6">
                    <label class="layui-form-label">库位名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="storehouseName" id="storehouseName" lay-verify="storehouseName" placeholder="请输入" autocomplete="off" class="layui-input" style="width:150px">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">库位类型</label>
                    <div class="layui-input-block" style="width:150px">
                        <select name="storehouseType" lay-filter="type" id="storehouseType" lay-verify="storehouseType">
                            <option value="">请选择</option>
                            <option value="正式库位">正式库位</option>
                            <option value="待入库区">待入库区</option>
                            <option value="待检库区">待检库区</option>
                            <option value="待出库区">待出库区</option>
                        </select>
                    </div>
                </div>
            </div>


            <div class="layui-row" style="margin-bottom: 20px;">
                <div class="layui-col-xs6">
                    <label class="layui-form-label">是否默认</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="isDefaults" id="isDefaults" value=true lay-verify="isDefaults">

                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">是否禁用</label>
                    <div class="layui-input-block" style="width:150px">
                        <input type="checkbox" name="isDisable" id="isDisable" value=true lay-verify="isDisable">

                    </div>
                </div>
            </div>

            <div style="text-align:center;">
                <button class="layui-btn layui-bg-blue" lay-submit lay-filter="demo-verify">提交</button>
                <button class="layui-btn-primary layui-btn" onclick="location.href='/Foundation/GetStorehouseList">取消</button>
            </div>

        </form>
    </div>
</div>







<script>

    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$;

        var token = localStorage.getItem("token");

        //获取跳转过来的id
        var urlParams = new URLSearchParams(window.location.search);
        var id = urlParams.get('id');
        console.log(id);

        //验证
        form.verify({
        StorehouseCode: function (value, elem) {

                if (!value) {
                    return "请输入库位编号"
                } ;

            },
        StorehouseName: function (value, elem) {
                if (!value) {
                    return "请输入库位名称"
                }
            },
        StorehouseType: function (value, elem) {
                if (!value) {
                    return "请输入库位类型"
                }
            },
        WarehouseId: function (value, elem) {
                if (!value) {
                    return "请选择仓库"
                }
            },
        IsDisable: function (value, elem) {
                if (!value) {
                    return "请选择是否禁用"
                }

            },
        IsDefaults: function (value, elem) {
            if (!value) {
                return "请选择是否默认"
            }

        },

        });

        // 动态加载仓库
        $.ajax({
            url: '/Foundation/GetWare',
            type: "get",
            data: { Token: token },
            success: function (res) {
                var resdata = JSON.parse(res);
                // 假设服务器返回的已经是 JSON 格式，无需手动解析
                // 直接从 res 中获取 data 数据
                var selectData = resdata.data;
                console.log(selectData);  // 调试输出返回的结果
                if (Array.isArray(selectData) && selectData.length > 0) {
                    // 为 select 渲染选项
                    var selectHtml = '<option value="">请选择</option>';
                    selectData.forEach(function (item) {
                        selectHtml += `<option value=${item.warehouseId}>${item.warehouseName}</option>`;
                    });

                    // 更新 select 元素的 HTML
                    $('#warehouseId').html(selectHtml);

                    // 渲染 select 元素
                    form.render('select');
                } else {
                    console.warn('返回的 selectData 为空或不是有效的数组', selectData);
                }

                // 为 select 绑定事件
                form.on('select(warehouseId)', function (data) {
                    console.log('选中的值：', data.value);
                });
            },
            error: function (err) {
                console.error('请求失败:', err);  // 更详细的错误日志
            }
        });

        var data = {
            Id: id,
            Token: token
        }
        //反填
        $.ajax({
           
            url: '/Foundation/GetStoreInfo',
            type: 'get',
            data: data,
            success: function (res) {
                var resdata = JSON.parse(res);
                var data = resdata.data;
                console.log(data);
                $("#storehouseCode").val(data.storehouseCode);
                layui.form.render('select');
                $("#warehouseId").val(data.warehouseId);
                layui.form.render('select');
                $("#storehouseName").val(data.storehouseName);
                $("#storehouseType").val(data.storehouseType);
                layui.form.render('select');
                $('input[name="isDefaults"]').prop('checked', data.isDefaults ? true : false);
                $('input[name="isDisable"]').prop('checked', data.isDisable ? true : false);
              
            }
        })


        //提交
        form.on('submit(demo-verify)', function (data) {
            console.log(data);
            var storehouseCode = data.field.storehouseCode;
            var warehouseId = data.field.warehouseId;
            var storehouseName = data.field.storehouseName;
            var storehouseType = data.field.storehouseType;
            var isDefaults = data.field.isDefaults?true : false;
            var isDisable = data.field.isDisable ? true : false;
            var field = {
                storehouseId:id,
                storehouseCode: storehouseCode,
                warehouseId: warehouseId,
                storehouseName: storehouseName,
                storehouseType: storehouseType,
                isDefaults: isDefaults,
                isDisable: isDisable,
                Token: token
            }

            console.log(field);
          
            $.ajax({
                url: '/Foundation/UptStore',
                type: 'PUT',
                data: field,
                success: function (res) {
                    var s = JSON.parse(res);
                    if (s.data > 0) {
                        layer.msg('修改成功', { icon: 1 }, function () {
                            location.href = '/Foundation/GetStorehouseList';
                        });

                    } else {

                    }
                }
            })
            return false; // 阻止表单跳转。
        })


    })



</script>

